package com.ruoyi.eqmonitor.smdtv.service.impl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.eqmonitor.smdtv.domain.SmdtvSewagePlant;
import com.ruoyi.eqmonitor.smdtv.mapper.SmdtvSewagePlantMapper;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvSewagePlantService;
import com.ruoyi.eqmonitor.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 污水厂Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class SmdtvSewagePlantServiceImpl extends ServiceImpl<SmdtvSewagePlantMapper, SmdtvSewagePlant> implements ISmdtvSewagePlantService {
    
    @Autowired
    private SmdtvSewagePlantMapper smdtvSewagePlantMapper;

    @Override
    public SmdtvSewagePlant selectSmdtvSewagePlantById(Long id){
        return smdtvSewagePlantMapper.selectSmdtvSewagePlantById(id);
    }
      
    @Override
    public List<SmdtvSewagePlant> selectSmdtvSewagePlantList(SmdtvSewagePlant smdtvSewagePlant){
        return smdtvSewagePlantMapper.selectSmdtvSewagePlantList(smdtvSewagePlant);
    }

    @Override
    public boolean toLeadJSON(InputStream inputStream) throws Exception {
        String sFile = FileUtil.ReadFile((FileInputStream) inputStream);
        JSONObject jsonObject = JSON.parseObject(sFile);
        Object features = jsonObject.get("features");
        String featuresString = JSONArray.toJSONString(features);
        List<String> featuresList= JSONArray.parseArray(featuresString,String.class);
        ArrayList<SmdtvSewagePlant> list = new ArrayList<>();
        for (String s : featuresList){
            JSONObject sjsonObject = JSON.parseObject(s);
            Object properties = sjsonObject.get("properties");
            String s1 = properties.toString();
            JSONObject map = JSON.parseObject(s1);
            SmdtvSewagePlant plant = new SmdtvSewagePlant();
            plant.setName(map.get("名称").toString());
            plant.setSsjd(map.get("所属街道").toString());
            plant.setType(map.get("类型").toString());
            plant.setX(map.get("经度").toString());
            plant.setY(map.get("纬度").toString());
            plant.setSjId(map.get("上级ID").toString());
            plant.setSjName(map.get("上级名称").toString());
            plant.setFw(map.get("范围").toString());
            plant.setGxlj(map.get("管线路径").toString());
            plant.setBzwws(map.get("泵站或污水").toString());

            list.add(plant);
        }
        //批量导入
        return this.saveBatch(list);
    }
}
